package com.example.cmh.common;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import org.apache.commons.io.FilenameUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.UUID;

public class UploadUtil {
    public static final String ALI_DOMAIN = "https://cmh-springboot-vue3.oss-cn-beijing.aliyuncs.com/";

    public static String uploadImage(MultipartFile file) throws IOException {
        String originalFileName = file.getOriginalFilename();
        String ext = "." + FilenameUtils.getExtension(originalFileName);
        String uuid = UUID.randomUUID().toString().replace("-","");
        String fileName = uuid + ext;
        String endpoint = "https://oss-cn-beijing.aliyuncs.com";
        String accessKeyId = "LTAI5tBUGLbsnbK8D9dHCaaR";
        String accessKeySecret = "xzd66dEwwM3xIk715QBpicMkQTzXMe";
        OSS ossClient = new OSSClientBuilder().build(endpoint,accessKeyId,accessKeySecret);
        ossClient.putObject(
                "cmh-springboot-vue3",
                fileName,
                file.getInputStream()
        );
        ossClient.shutdown();
        return ALI_DOMAIN+fileName;
    }
}
